//
//  WISHomeTabBarView.m
//  WisdomStudy
//
//  Created by  androidlongs on 2017/8/26.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "WISHomeTabBarView.h"
#import "WISHomeTabBarModel.h"


@interface WISHomeTabBarView()

//tabBar的背景
@property(nonatomic,strong) UIImageView *tabbarBackgroundImageView;

//tabBar条目数据集合
@property(nonatomic,strong) NSMutableArray *dataList;

//上一个点击的Item
@property(nonatomic,strong) UIView *lastClickUiView ;


@property(nonatomic,strong) UIColor *itemSelectColor ;
@property(nonatomic,strong) UIColor *itemNormalColor ;
@end

@implementation WISHomeTabBarView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
-(instancetype) initWithItem:(NSMutableArray *)itemList :(CGRect)frame :(UIColor *)normalColor :(UIColor *)selectColor{
    self.dataList = itemList;
    self.itemSelectColor = selectColor;
    self.itemNormalColor = normalColor;
    return [self initWithFrame:frame];
}



-(void)selectItem:(NSInteger)index{
    NSInteger tag = index;
    //上一次点击的View的tag
    NSInteger lastClickTag = self.lastClickUiView.tag;
    
    if (tag==lastClickTag) {
        //同一次点击
    }else{
        
        for (NSInteger i=0; i<[self subviews].count; i++) {
            UIView *itemView =[self subviews][i];
            NSInteger cusTag = itemView.tag;
            
            if (cusTag==tag) {
                self.lastClickUiView = itemView;
                
                for (NSInteger j=0; j<[itemView subviews].count; j++) {
                    UIView *child = [itemView subviews][j];
                    if ([child isKindOfClass:[UIImageView class]]) {
                        [((UIImageView *)child) setImage:[UIImage imageNamed:((WISHomeTabBarModel*)self.dataList[tag]).imagePressName]];
                        
                        //设置动画
                        [UIView animateWithDuration:0.2 animations:^{
                            child.transform=CGAffineTransformMakeScale(1.2, 1.2);
                        } completion:^(BOOL finished) {
                            [UIView animateWithDuration:0.2 animations:^{
                                //恢复原始状态
                                child.transform = CGAffineTransformIdentity;
                            }];
                        }];
                        
                        //代理回调
                        if ([self.delegate respondsToSelector:@selector(tabBar)]) {
                            [self.delegate tabbar:self clickButton:tag];
                        }
                        //block回调
                        if(self.block){
                            self.block(self,tag);
                        }
                        
                        
                    }else if([child isKindOfClass:[UILabel class]]){
                        [((UILabel *)child) setTextColor:[UIColor blueColor]];
                    }
                    
                }
            }else{
                for (NSInteger j=0; j<[itemView subviews].count; j++) {
                    UIView *child = [itemView subviews][j];
                    if ([child isKindOfClass:[UIImageView class]]) {
                        [((UIImageView *)child) setImage:[UIImage imageNamed:((WISHomeTabBarModel*)self.dataList[itemView.tag]).imageNormalName]];
                    }else if([child isKindOfClass:[UILabel class]]){
                        [((UILabel *)child) setTextColor:[UIColor grayColor]];
                    }
                    
                }
            }
        }
        
    }
    
}


//初始化设置tabbar的背景
-(UIImageView *)tabbarBackgroundImageView{
    if (!_tabbarBackgroundImageView) {
        _tabbarBackgroundImageView=[[UIImageView alloc]init];
        [_tabbarBackgroundImageView setBackgroundColor:[UIColor grayColor]];
    }
    return _tabbarBackgroundImageView;
}

//创建Item 并将Item添加到 TabBar（其实是自定义的UIView）中
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    //循环创建Item
    for (NSInteger i=0; i<self.dataList.count; i++) {
        //取出Item数据
        WISHomeTabBarModel *itemModel = self.dataList[i];
        //构建Item
        UIView *iteView = [[UIView alloc]init];
        
        //创建 UIImageView
        UIImageView *itemImageView = [[UIImageView alloc]init];
        //创建  UILabel
        UILabel *label = [[UILabel alloc]init];
        //设置显示字体
        label.text = itemModel.name;
        label.textAlignment=NSTextAlignmentCenter;
        //设置字体显示大小
        label.font=[UIFont italicSystemFontOfSize:10];
        
        
        [iteView addSubview:itemImageView];
        [iteView addSubview:label];
        
        //设置tag
        iteView.tag = WISHomeItemTypeDefault+i;
        
        //设置手势监听
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickItem:)];
        [tap setNumberOfTapsRequired:1];
        [tap setNumberOfTouchesRequired:1];
        [iteView addGestureRecognizer:tap];
        
        
        //设置默认
        if (i==0) {
            self.lastClickUiView = iteView;
            //默认图标
            [itemImageView setImage:[UIImage imageNamed:itemModel.imagePressName]];
            //默认text颜色
            label.textColor=self.itemSelectColor;
            
        }else{
            [itemImageView setImage:[UIImage imageNamed:itemModel.imageNormalName]];
            label.textColor=self.itemNormalColor;
        }
        
        [self addSubview:iteView];
        
    }
    
    
    return self;
}

//Item 的点击事件
-(void) clickItem:(UITapGestureRecognizer *) gesture{
    
    //当前点击的View
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)gesture;
    //当前点击的View的tag
    NSInteger tag = tap.view.tag;
    
    //上一次点击的View的tag
    NSInteger lastClickTag = self.lastClickUiView.tag;
    
    if (tag==lastClickTag) {
        //同一次点击
    }else{
        if(self.delegate&&[self.delegate respondsToSelector:@selector(tabbar:clickButton:)]){
            [self.delegate tabbar:self clickButton:tag];
        }
        
        for (NSInteger i=0; i<[self subviews].count; i++) {
            UIView *itemView =[self subviews][i];
            NSInteger cusTag = itemView.tag;
            
            if (cusTag==tag) {
                self.lastClickUiView = itemView;
                
                for (NSInteger j=0; j<[itemView subviews].count; j++) {
                    UIView *child = [itemView subviews][j];
                    if ([child isKindOfClass:[UIImageView class]]) {
                        [((UIImageView *)child) setImage:[UIImage imageNamed:((WISHomeTabBarModel*)self.dataList[tag]).imagePressName]];
                        
                        //设置动画
                        //                        [UIView animateWithDuration:0.1 animations:^{
                        //                            child.transform=CGAffineTransformMakeScale(1.1, 1.1);
                        //                        } completion:^(BOOL finished) {
                        //                            [UIView animateWithDuration:0.35 animations:^{
                        //                                //恢复原始状态
                        //                                child.transform = CGAffineTransformIdentity;
                        //                            }];
                        //                        }];
                        
                        [UIView animateKeyframesWithDuration:0.5 delay:0 options:0 animations: ^{
                            
                            [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 3.0 animations: ^{
                                child.transform = CGAffineTransformMakeScale(1.1, 1.1);
                            }];
                            [UIView addKeyframeWithRelativeStartTime:1/3.0 relativeDuration:1/3.0 animations: ^{
                                child.transform = CGAffineTransformMakeScale(0.9, 0.9);
                            }];
                            [UIView addKeyframeWithRelativeStartTime:2/3.0 relativeDuration:1/3.0 animations: ^{
                                child.transform = CGAffineTransformMakeScale(1.0, 1.0);
                            }];
                        } completion:nil];
                        //代理回调
                        if ([self.delegate respondsToSelector:@selector(tabBar)]) {
                            [self.delegate tabbar:self clickButton:tag];
                        }
                        //block回调
                        if(self.block){
                            self.block(self,tag);
                        }
                        
                        
                    }else if([child isKindOfClass:[UILabel class]]){
                        //设置选中的颜色
                        [((UILabel *)child) setTextColor:self.itemSelectColor];
                    }
                    
                }
            }else{
                for (NSInteger j=0; j<[itemView subviews].count; j++) {
                    UIView *child = [itemView subviews][j];
                    if ([child isKindOfClass:[UIImageView class]]) {
                        [((UIImageView *)child) setImage:[UIImage imageNamed:((WISHomeTabBarModel*)self.dataList[itemView.tag]).imageNormalName]];
                    }else if([child isKindOfClass:[UILabel class]]){
                        [((UILabel *)child) setTextColor:self.itemNormalColor];
                    }
                    
                }
            }
        }
        
    }
    
    DLog(@"click %ld ",(long)tag);
}
//测量设置每一个条目的Frame
-(void)layoutSubviews{
    [super layoutSubviews];
    //设置背景的frame
    self.tabbarBackgroundImageView.frame = self.bounds;
    
    //每一个item的宽度
    CGFloat itemWidth = self.bounds.size.width/self.dataList.count;
    for (NSInteger i=0; i<[self subviews].count; i++) {
        UIView *view = [self subviews][i];
        
        DLog(@"view tag is %ld ",(long)view.tag);
        
        view.frame = CGRectMake((view.tag)*itemWidth, 0, itemWidth, self.frame.size.height);
        
        for (NSInteger j=0; j<[view subviews].count; j++) {
            UIView *childView = [view subviews][j];
            if([childView isKindOfClass:[UIImageView class]]){
                WISHomeTabBarModel *tabBarModel = self.dataList[view.tag];
                childView.frame=CGRectMake(view.bounds.size.width/2-tabBarModel.imageSize.width/2, 6, tabBarModel.imageSize.width, tabBarModel.imageSize.height);

            }else if([childView isKindOfClass:[UILabel class]]){
                childView.frame=CGRectMake(view.bounds.size.width/2-41, 30, 82, 22);
            }
        }
        
    }
}

@end
